/**
 * 
 */
package com.example.radioclub.tasks.http;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.radioclub.data.Constants;
import com.example.radioclub.interfaces.http.IHttpRequestResponseDownloader;

import android.os.AsyncTask;
import android.util.Log;

/**
 * This is downloader for Http Get request.
 * 
 * @author wizard
 *
 */
public class HttpGetResponseDownloader extends AsyncTask<String, Void, String>{
	private final String TAG = Constants.APP_TAG + "Http Get Response Downloader";
	private IHttpRequestResponseDownloader listener;
	
	public HttpGetResponseDownloader(IHttpRequestResponseDownloader listener) {
		// TODO Auto-generated constructor stub
		this.listener = listener;
	}
	
	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub
		String url = (params.length == 1 ? params[0] : null);
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
		try {
			return httpClient.execute(httpGet, responseHandler);			
		} catch (Exception e) {
			e.printStackTrace();
			Log.i(TAG, e.getMessage());
		}
		return null;
	}
	
	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		Log.i(TAG, result);
		this.listener.onHttpResponseFinished(result);
		super.onPostExecute(result);
	}

}
